#include "random.h"
#include <cstdlib>
#include <ctime>

bool Random::_seeded = false;
uint Random::_seed   = 0;

int Random::Int( int lessThan ) {
    if ( !_seeded ) {
        _seed = uint( time( 0 ) );
        srand( _seed );
        _seeded = true;
    }

    if ( lessThan == 0 )
        return 0;

    bool neg = false;
    if ( lessThan < 0 ) {
        lessThan = -lessThan;
        neg = true;
    }

    int r = rand() % lessThan;
    return neg ? -r : r;
}

int Random::Int( int nDice, int dieMax, int base )
{
    bool neg = false;
    if ( nDice < 0 ) {
        nDice = -nDice;
        neg = !neg;
    }
    if ( dieMax < 0 ) {
        dieMax = -dieMax;
        neg = !neg;
    }

    int r = 0;
    for ( int i = 0; i < nDice; i++ ) {
        r += 1 + Int( dieMax - 1 );
    }
    return base + ( neg ? -r : r );
}

uint Random::GetSeed()
{
    return _seed;
}

void Random::SetSeed( uint seed )
{
    _seed = seed;
    srand( _seed );
    _seeded = true;
}
